//
//  WBBasicView.swift
//  weibo
//
//  Created by  郭航 on 16/6/25.
//  Copyright © 2016年 郭航. All rights reserved.
//

import UIKit
import SnapKit

class WBBasicView: UIView {
    
    private lazy var feedImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    private lazy var maskImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    private lazy var iconImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = APPTintColor
        label.textAlignment = .Center
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        
        return label
    }()
    
    private lazy var loginButon: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: #selector(WBBasicView.buttonClick), forControlEvents: .TouchUpInside)
        
        button.setTitle("登录", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(APPTintColor, forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return button
    }()
    
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: #selector(WBBasicView.buttonClick), forControlEvents: .TouchUpInside)
        
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(APPTintColor, forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return button
    }()
    
    var buttonCallBack: (() -> ())?
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        addSubview(feedImageView)
        feedImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        addSubview(maskImageView)
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        addSubview(iconImageView)
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) in
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
            make.centerX.equalTo(iconImageView)
        }
        
        addSubview(loginButon)
        loginButon.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        
        addSubview(registerButton)
        registerButton.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(loginButon)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func buttonClick() -> () {
        buttonCallBack?()
    }
    
    func feedImageAnimation() -> Void {
        let basicAni = CABasicAnimation(keyPath: "transform.rotation")
        
        basicAni.duration = 10
        basicAni.toValue = M_PI * 2
        basicAni.repeatCount = MAXFLOAT
        basicAni.removedOnCompletion = false
        
        feedImageView.layer.addAnimation(basicAni, forKey: nil)
    }
    
    func setIconImageAddMessage(imageName: String?, message: String?) -> () {
        if let _ = imageName, let _ = message {
            iconImageView.image = UIImage(named: imageName!)
            messageLabel.text = message
            
            feedImageView.hidden = true
        } else {
            feedImageAnimation()
        }
    }

}
